<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="../assets/js/vue.js"></script>
    <title>Example Event Demo</title>
</head>

<body>
    <h1>Example Event Demo</h1>
    <hr>
    <div id="app">
        <p>{{num}}</p>
        <button @click="add">add</button>

    </div>
    <p>
        <button onclick="reduce()">reduce</button>
    </p>
    <p>
        <button onclick="reduceOnce()">reduceOnce</button>
    </p>
    <p>
        <button onclick="off()">off</button>
    </p>


    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                num: 1
            },
            methods: {
                add: function () {
                    this.num++;
                }
            }
        })
        //实例事件
        app.$on('reduce', function () {
            console.log('执行了reduce()');
            this.num--;
        });
        //只使用一次的实例方法
        app.$once('reduceOnce', function () {
            console.log('只执行一次的方法');
            this.num--;
        });
        //关闭事件
        function off() {
            app.$off('reduce');
        }
        //外部调用内部事件
        function reduce() {
            app.$emit('reduce');
        }

        function reduceOnce() {
            app.$emit('reduceOnce');
        }
    </script>
</body>

</html>